package DAO;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import Beans.CartaBean;
import HibernateUtils.HibernateUtil;

public class HibernateCartaDAO {
	private static HibernateCartaDAO instancia;
	private static SessionFactory sf;
	private HibernateCartaDAO(){
		
	}
	
	public static HibernateCartaDAO getInstancia(){
		if(instancia==null){
			sf=HibernateUtil.getSessionFactory();
			instancia=new HibernateCartaDAO();
		}
		return instancia;
	}
	
	public void grabarCarta(CartaBean carta) {
		Session sesion=sf.openSession();
		sesion.beginTransaction();
		sesion.persist(carta);
		sesion.flush();
		sesion.getTransaction().commit();
		sesion.close();
		
	}

	public CartaBean buscarCarta(CartaBean cartab) {
		Session session=sf.openSession();
		cartab=(CartaBean) session.get(CartaBean.class, cartab.getIdCarta());
		session.close();
		return cartab;
	}

	public void update(CartaBean carta) {
		Session sesion=sf.openSession();
		sesion.beginTransaction();
		sesion.update(carta);
		sesion.flush();
		sesion.getTransaction().commit();
		sesion.close();
		
	}
}
